package cn.rkylin.oms.base.refundReasonType.utils;

import cn.rkylin.oms.base.refundReasonType.utils.apprhandler.impl.*;
import com.alibaba.fastjson.JSONObject;
import com.taobao.api.internal.util.StringUtils;

import cn.rkylin.oms.base.refundReasonType.utils.apprhandler.ApprHandler;
import cn.rkylin.oms.common.consts.YesNoConst;

/**
 * 类名:RuleParseUtil <br/>
 * 作用: 规则解析装置. <br/>
 * 创建原因:	 将规则进行压缩存储. <br/>
 * 创建时间:     2017年10月31日 下午8:02:52 <br/>
 * @author   zhanghao
 * @version  v1.0 
 */
public class RuleParseUtil {
    public static int maxLength = 7;
    
    /**
     * parseFromUgly:基于压缩的规则字符串还原json字符串. <br/>
     *
     * @author zhanghao
     * @param uglyString
     * @return
     */
    public static String parseFromUgly(String uglyString) {
        String returnValue = "";
        JSONObject resultObject = new JSONObject();
        if(StringUtils.isNumeric(uglyString)) {
            String resultString = Integer.toBinaryString(Integer.valueOf(uglyString));
            StringBuffer b = new StringBuffer(resultString);  
            while(b.length()<maxLength){  
                b.insert(0, '0');  
            }  
            resultString = b.toString();
            JSONObject cRS = new JSONObject();
            if(isTrue(resultString.charAt(6))) {
                cRS.put("enable", "y");
            }else {
                cRS.put("enable", "n");
            }
            resultObject.put("cRS", cRS);
            JSONObject rRS = new JSONObject();
            if(isTrue(resultString.charAt(5))) {
                rRS.put("enable", "y");
                if(isTrue(resultString.charAt(4))) {
                    rRS.put("hPR", "y");
                    rRS.put("hSG", "n");
                }else {
                    rRS.put("hPR", "n");
                    rRS.put("hSG", "y");
                }
            }else {
                rRS.put("enable", "n");
                if(isTrue(resultString.charAt(4))) {
                    rRS.put("hPR", "y");
                    rRS.put("hSG", "n");
                }else {
                    rRS.put("hPR", "n");
                    rRS.put("hSG", "y");
                }
            }
            resultObject.put("rRS", rRS);
            
            JSONObject rE = new JSONObject();
            if(isTrue(resultString.charAt(2))) {
                rE.put("enable", "y");
                if(isTrue(resultString.charAt(1))) {
                    rE.put("rUM", "y");
                }else {
                    rE.put("rUM", "n");
                }
                if(isTrue(resultString.charAt(0))) {
                    rE.put("rB", "y");
                }else {
                    rE.put("rB", "n");
                }
            }else {
                rE.put("enable", "n");
            }
            resultObject.put("rE", rE);
        }
        
        return resultObject.toJSONString();
    }
    
    public static ApprHandler getApprHandler() {
        ApprHandler hasRefundSkuHandler = new HasRefundSkuHandler();
        ApprHandler hasReturnHandler = new HasReturnHandler();
        ApprHandler hasProcessingHandler = new HasProcessingReciptHandler();
        ApprHandler hasSendSkuHandler = new HasSendSkuHandler();
        ApprHandler noReturnHandler = new NoReturnHandler();
        ApprHandler hasBrokenHandler = new HasBrokenHandler();
        ApprHandler sysRefundExistsHandler = new SysRefundExistsHandler();
        ApprHandler isApprSetHandler = new IsApprSetHandler();
        ApprHandler apprSuccessHandler = new ApprSuccessHandler();

        hasReturnHandler.SetSuccessor(sysRefundExistsHandler);
        sysRefundExistsHandler.SetSuccessor(isApprSetHandler);
        isApprSetHandler.SetSuccessor(hasRefundSkuHandler);
        hasRefundSkuHandler.SetSuccessor(hasProcessingHandler);
        hasProcessingHandler.SetSuccessor(hasSendSkuHandler);
        hasSendSkuHandler.SetSuccessor(noReturnHandler);
        noReturnHandler.SetSuccessor(hasBrokenHandler);
        hasBrokenHandler.SetSuccessor(apprSuccessHandler);
        return hasReturnHandler;
    }
    
    public static ApprHandler getForceApprHandler() {
        ApprHandler hasReturnHandler = new HasReturnHandler();
        ApprHandler hasRefundSkuHandler = new HasRefundSkuHandler();
        ApprHandler isForceApprHandler = new IsForceApprHandler();
        ApprHandler hasProcessingHandler = new HasProcessingReciptHandler();
        ApprHandler hasSendSkuHandler = new HasSendSkuHandler();
        ApprHandler noReturnHandler = new NoReturnHandler();
        ApprHandler hasBrokenHandler = new HasBrokenHandler();
        ApprHandler sysRefundExistsHandler = new SysRefundExistsHandler();
        ApprHandler isApprSetHandler = new IsApprSetHandler();
        ApprHandler apprSuccessHandler = new ApprSuccessHandler();

        hasReturnHandler.SetSuccessor(sysRefundExistsHandler);
        sysRefundExistsHandler.SetSuccessor(isApprSetHandler);
        isApprSetHandler.SetSuccessor(isForceApprHandler);
        isForceApprHandler.SetSuccessor(hasRefundSkuHandler);
        hasRefundSkuHandler.SetSuccessor(hasProcessingHandler);
        hasProcessingHandler.SetSuccessor(hasSendSkuHandler);
        hasSendSkuHandler.SetSuccessor(noReturnHandler);
        noReturnHandler.SetSuccessor(hasBrokenHandler);
        hasBrokenHandler.SetSuccessor(apprSuccessHandler);
        return hasReturnHandler;
    }
    
    /**
     * extractUgly:根据传入的JSON字符串返回压缩数据 <br/>
     *
     * @author zhanghao
     * @param jsonStrng
     * @return
     */
    public static int extractUgly(String jsonStrng) {
        int result = 0;
        JSONObject jsonObject = JSONObject.parseObject(jsonStrng);
        if(YesNoConst.YES.equalsIgnoreCase(jsonObject.getJSONObject("cRS").getString("enable"))) {
            result+=1<<0;
        };
        
        if(YesNoConst.YES.equalsIgnoreCase(jsonObject.getJSONObject("rRS").getString("enable"))) {
            result+=1<<1;
            if(YesNoConst.YES.equalsIgnoreCase(jsonObject.getJSONObject("rRS").getString("hPR"))) {
                result+=1<<2;
            }else {
                result+=1<<3;
            }
        }

        if(YesNoConst.YES.equalsIgnoreCase(jsonObject.getJSONObject("rE").getString("enable"))) {
            result+=1<<4;
            if(YesNoConst.YES.equalsIgnoreCase(jsonObject.getJSONObject("rE").getString("rUM"))) {
                result+=1<<5;
            }
            
            if(YesNoConst.YES.equalsIgnoreCase(jsonObject.getJSONObject("rE").getString("rB"))) {
                result+=1<<6;
            }
            
        }
//        // 变更 总是检查退货异常
//        result+=1<<6;
        return result;
    }
    
    private static boolean isTrue(char compareChar) {
        if('1'==compareChar) {
            return true;
        }else {
            return false;
        }
    }
}

